package com.kx.demo.concurrent;

/**
 * @author kx
 * @date 2020/4/12
 */
public class InterruptDemo2 {

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();

        thread.start();

        Thread.sleep(1000);

        thread.setShouldRun(false);

        thread.interrupt();

    }

    private static class MyThread extends Thread {
        private Boolean shouldRun = true;

        @Override
        public void run() {
            while (shouldRun){
                try {
                    System.out.println("线程1在执行");
                    sleep(30 * 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void setShouldRun(Boolean shouldRun) {
            this.shouldRun = shouldRun;
        }
    }

}
